package com.lepai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author weihaolong
 * @date 2018-11-19 15:49
 * @description
 */
public class DateUtils {

    public static final String STANDARD_DATE_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";

    public static final String STANDARD_DATE_FORMAT_2 = "yyyy-MM-dd";

    public static LocalDate getLocalDate(String date, String format){
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(format));
    }

    public static String getDateTimeToString(Date date){
        return getDateTimeToString(date, STANDARD_DATE_FORMAT_2);
    }

    public static String getDateTimeToString(Date date, String format){
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 判断是否是当天开始
     * @return
     */
    public static boolean isToday(Date beginDate){
        Date today = new Date();
        return getDateTimeToString(today).equals(getDateTimeToString(beginDate));
    }

    public static Date stringFormatDate(String date){
        return stringFormatDate(date, "yyyy-MM-dd HH:ss:mm");
    }

    /**
     * 将字符串转换成特定的时间格式
     * @return
     */
    public static Date stringFormatDate(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}